const Storage = require("ee-core/storage");
const electron = require("electron");
const constants = require("../shared/constants");
const engineUtil = require("../shared/utils/engine");
const Ps = require("ee-core/ps");
const UtilsIs = require("ee-core/utils/is");
const UA = require("../shared/ua");
const logger = require("ee-core/log");
const {
  APP_RUN_MODE,
  APP_THEME,
  EMPTY_STRING,
  ENGINE_RPC_PORT,
  IP_VERSION,
  LOGIN_SETTING_OPTIONS,
  NGOSANG_TRACKERS_BEST_IP_URL_CDN,
  NGOSANG_TRACKERS_BEST_URL_CDN,
  PROXY_SCOPES,
  PROXY_SCOPE_OPTIONS,
} = constants;

const {
  getConfigBasePath,
  getDhtPath,
  getMaxConnectionPerServer,
  getUserDownloadsPath,
} = engineUtil;

class engineManager {
  constructor() {
    this.systemConfig = {};
    this.userConfig = {};
    this.init();
  }
  init() {
    this.initSystemConfig();
    this.initUserConfig();
  }
  initSystemConfig() {
    this.systemConfig = Storage.connection("systemConfig");
    this.systemConfig.setItem("system", {
      "all-proxy": EMPTY_STRING,
      "allow-overwrite": false,
      "auto-file-renaming": true,
      "bt-exclude-tracker": EMPTY_STRING,
      "bt-force-encryption": false,
      "bt-load-saved-metadata": true,
      "bt-save-metadata": true,
      "bt-tracker": EMPTY_STRING,
      continue: true,
      "dht-file-path": getDhtPath(IP_VERSION.V4),
      "dht-file-path6": getDhtPath(IP_VERSION.V6),
      "dht-listen-port": 26701,
      dir: getUserDownloadsPath(),
      "enable-dht6": true,
      "follow-metalink": true,
      "follow-torrent": true,
      "listen-port": 21301,
      "max-concurrent-downloads": 5,
      "max-connection-per-server": getMaxConnectionPerServer(),
      "max-download-limit": 0,
      "max-overall-download-limit": 0,
      "max-overall-upload-limit": 0,
      "no-proxy": EMPTY_STRING,
      "pause-metadata": false,
      pause: true,
      "rpc-listen-port": ENGINE_RPC_PORT,
      "rpc-secret": EMPTY_STRING,
      "seed-ratio": 2,
      "seed-time": 2880,
      split: getMaxConnectionPerServer(),
      "user-agent": UA.chrome,
    });

    // logger.info('initSystemConfig', this.systemConfig.getItem('system'))
  }

  initUserConfig() {
    this.userConfig = Storage.connection("userConfig");
    this.userConfig.setItem("user", {
      "auto-check-update": UtilsIs.macOS(),
      "auto-hide-window": false,
      "auto-sync-tracker": true,
      "enable-upnp": true,
      "engine-max-connection-per-server": getMaxConnectionPerServer(),
      "favorite-directories": [],
      "hide-app-menu": UtilsIs.windows() || UtilsIs.linux(),
      "history-directories": [],
      "keep-seeding": false,
      "keep-window-state": false,
      "last-check-update-time": 0,
      "last-sync-tracker-time": 0,
      // locale: electron.getLocale(),
      "log-level": "warn",
      "new-task-show-downloading": true,
      "no-confirm-before-delete-task": false,
      "open-at-login": false,
      protocols: { magnet: true, thunder: false },
      proxy: {
        enable: false,
        server: EMPTY_STRING,
        bypass: EMPTY_STRING,
        scope: PROXY_SCOPE_OPTIONS,
      },
      "resume-all-when-app-launched": false,
      "run-mode": APP_RUN_MODE.STANDARD,
      "show-progress-bar": true,
      "task-notification": true,
      theme: APP_THEME.AUTO,
      "tracker-source": [
        NGOSANG_TRACKERS_BEST_IP_URL_CDN,
        NGOSANG_TRACKERS_BEST_URL_CDN,
      ],
      "tray-theme": APP_THEME.AUTO,
      "tray-speedometer": UtilsIs.macOS(),
      "update-channel": "latest",
      "window-state": {},
    });
  }
}

module.exports = engineManager;
